﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PermissionsManager.DAL
{
    public class BaseRepository<T, TS> where T : class, new()
                                       where TS : DbContext, new()
    {
        private DbContext db = DbContextFactory<TS>.GetCurrentDbContext();

        /// <summary>
        /// 通用新增方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(T entity)
        {

            db.Set<T>().Add(entity);
            return db.SaveChanges() > 0;

        }

        /// <summary>
        /// 通用删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            db.Entry(entity).State = EntityState.Deleted;
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 通用批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(List<T> entitys)
        {
            foreach (var item in entitys)
            {
                var entity = db.Set<T>().Find(item);
                db.Set<T>().Remove(entity);
            }
            return db.SaveChanges() > 0;
        }
        /// <summary>
        /// 通用批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(params int[] ids)
        {
            foreach (var item in ids)
            {
                var entity = db.Set<T>().Find(item);
                db.Set<T>().Remove(entity);
            }
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 通用修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            db.Entry(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 通用查询集合
        /// </summary>
        public List<T> QueryList(Expression<Func<T, bool>> expression = null)
        {
            return db.Set<T>().Where(expression).ToList();
        }

        /// <summary>
        /// 查询返回对象
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public T Query(Expression<Func<T, bool>> expression = null)
        {
            return db.Set<T>().Where(expression).SingleOrDefault();
        }

        /// <summary>
        /// 判断记录是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public bool Exists(Expression<Func<T, bool>> expression = null)
        {
            return db.Set<T>().Where(expression).Any();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        public List<T> QueryPageList<S>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereExpression, Expression<Func<T, S>> orderExpression, bool isDsc = true)
        {
            if (!isDsc)
            {
                return db.Set<T>().Where(whereExpression)
                                  .OrderByDescending(orderExpression)
                                  .Skip((pageIndex - 1) * pageSize)
                                  .Take(pageSize).ToList();
            }
            return db.Set<T>().Where(whereExpression)
                              .OrderBy(orderExpression)
                              .Skip((pageIndex - 1) * pageSize)
                              .Take(pageSize).ToList();
        }

        /// <summary>
        /// 返回总数
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int GetCount(Expression<Func<T, bool>> whereExpression, out int count)
        {
            return count = db.Set<T>().Where(whereExpression).Count();
        }

        /// <summary>
        /// 通过sql语句查询
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="sql"></param>
        /// <param name="paramsobject"></param>
        /// <returns></returns>
        public List<TS> GetListBySql<S>(string sql, params object[] paramsobject)
        {
            return db.Set<TS>().SqlQuery(sql, paramsobject).ToList();
        }
    }
}
